﻿namespace POBR.ImageProcessor
{
	public class Binarizer
	{
		public Picture Binarize(Picture picture)
		{
			var result = picture.Clone();

			for (int x = 0; x != result.Width; ++x)
			{
				for (int y = 0; y != result.Height; ++y)
				{
					var pixel = picture.GetPixel(x, y);

					if (pixel[0] > 80 && pixel[0] > pixel[1] * 5 / 4 && pixel[0] > pixel[2])
					{
						result.SetRed(x, y);
					}
					else if (pixel[2] > 80 && pixel[2] > pixel[0] * 5 / 4 && pixel[2] > pixel[1])
					{
						result.SetBlue(x, y);
					}
					else if (pixel[0] > 60 && pixel[1] > 60 && pixel[2] > 60 &&
							 pixel[0] * 9 / 10 < pixel[1] &&
							 pixel[1] * 9 / 10 < pixel[2] &&
							 pixel[2] * 9 / 10 < pixel[0])
					{
						result.SetWhite(x, y);
					}
					else
					{
						result.SetBlack(x, y);
					}
				}
			}

			return result;
		}
	}
}
